@extends('Admin.base')
@section('css')
    <link rel="stylesheet" href="/Admin/layuiadmin/css/extend/formSelects-v4.css" media="all">
@endsection
@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <fieldset class="layui-elem-field layui-field-title">
                <legend>优惠券发放</legend>
            </fieldset>
        </div>
        <div class="layui-card-body">
            <form class="layui-form" id="layui-form" action="{{route('marketing.store')}}" method="post">
                @include('Admin.marketing._form')
            </form>
        </div>
    </div>


    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button type="button" class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中数据</button>
            <button type="button" class="layui-btn layui-btn-sm" lay-event="clearCheckData">清除被选数据</button>
        </div>
    </script>
    <script type="text/html" id="inviteInfo">
        <div>
            @{{# if(d.inviteInfo != ''){  }}
            @{{d.inviteInfo ? d.inviteInfo.realname : "" }}<br>@{{d.inviteInfo ? d.inviteInfo.number : ""}}<br>@{{d.inviteInfo ? d.inviteInfo.user_department.department_name : ""}}
            @{{# } }}
        </div>

    </script>
@endsection
@section('script')
    <script src="/js/ajax.js"></script>
    <script>
        layui.use(['formSelects','form','laydate','tableSelect'], function(){
            var formSelects = layui.formSelects;
            var tableSelect = layui.tableSelect;
            var laydate = layui.laydate;
            var form = layui.form;
            var $ = layui.$
                ,table = layui.table,
                user_lists = [[],[]];
            if ($("input[name='rule_type']:checked").val() == 1)  {
                $(".count1").show();
                $(".count2").hide();
            }else if($("input[name='rule_type']:checked").val() == 2){
                $(".count1").hide();
                $(".count2").show();
            }else {
                $(".count2").hide();
                $(".count1").hide();
            }
            form.render();
            form.on('radio(radio)', function (data) {
                if (data.value == 2)  {
                    $(".count2").show();
                    $(".count1").hide()
                    $("input[name='user_list']").val('');
                    $(".user_name_list").html('');
                    user_lists = [[],[]];
                }else if(data.value == 1){
                    $(".count2").hide();
                    $(".count1").show();
                    $("input:checkbox").removeAttr("checked");
                }else {
                    $(".count1").hide();
                    $(".count2").hide();
                    $("input:checkbox").removeAttr("checked");
                    $("input[name='user_list']").val('');
                    $(".user_name_list").html('');
                    user_lists = [[],[]];
                        console.log(user_lists)
                }
                form.render();
            });


            //日期时间范围
            laydate.render({
                elem: '#datetime-pick'
                ,type: 'datetime'
                ,range: '~'
            });
            table.render({
                elem: '#test'
                ,toolbar: '#toolbarDemo'
                ,defaultToolbar: []
                ,url: "{{url('/admin/user/table')}}"
                ,method: "post"
                ,totalRow: true
                ,page: true //开启分页
                ,limit:10
                ,cols: [[
                    {type: 'checkbox', }
                    ,{field: 'avatar', title: '头像',templet:function(d){return '<img src="'+ d.avatar + '" />'}}
                    ,{field: 'nickname', title: '会员信息', templet: '<div>@{{d.nickname ? d.nickname : "" }}<br>@{{d.phone ? d.phone : ""}}</div>'}
                    ,{field: 'sex_name', title: '性别' }
                    ,{field: 'created_at', title: '注册时间'}
                    ,{field: 'calcLevel', title: '会员等级' , templet:'<div>@{{d.calcLevel ? d.calcLevel : "" }}<br>@{{d.surplus_time ? d.surplus_time : ""}}</div>'}
                    ,{field: 'user_type_name', title: '会员身份'}
                    ,{field: 'user_type_name', title: '所属员工信息', templet:'#inviteInfo'}
                ]]
            });

            //工具栏事件
            table.on('toolbar(test)', function(obj){
                var checkStatus = table.checkStatus(obj.config.id),
                    user_list = $("input[name='user_list']"),
                    user_name_list = $(".user_name_list");

                switch(obj.event){
                    case 'getCheckData':
                        var data = checkStatus.data,
                            selectedUserId = '',
                            selectedName = '',
                            msgName  = ''
                        newArr(data)
                        console.log(user_lists)
                        layui.each(user_lists[0], function (index, item) {
                            selectedUserId += item + ',';
                        });
                        layui.each(user_lists[1], function (index, item) {
                            selectedName += item + ',';
                        });
                        layui.each(data, function (index, item) {
                            msgName += item.nickname + ',';
                        });
                        if (selectedUserId){
                            layer.msg('添加用户：' + msgName, {
                                btn: ['确认', '取消']
                                ,time: 5000
                                ,yes: function(index, layero){
                                    user_list.val(selectedUserId);
                                    user_name_list.html(selectedName);
                                    layer.closeAll();
                                }
                                ,btn2: function(index, layero){
                                    layer.closeAll();
                                }
                            });
                        }
                        break;
                    case 'clearCheckData':
                        if (user_list.val()){
                            layer.msg('是否全部取消', {
                                btn: ['确认', '取消']
                                ,time: 5000
                                ,yes: function(index, layero){
                                    user_list.val('');
                                    user_name_list.html('');
                                    layer.closeAll();
                                }
                                ,btn2: function(index, layero){
                                    layer.closeAll();
                                }
                            });
                        }
                        break;
                };
            });
            function newArr(array){
                //一个新的数组
                var length = array.length;
                //遍历当前数组
                for(var i = 0; i < length; i++){
                    if (user_lists[0].indexOf(array[i]['id']) == -1){
                        user_lists[0].push(array[i]['id'])
                        user_lists[1].push(array[i]['nickname'])
                    };
                }
                return true;
            }
            //监听搜索
            form.on('submit(LAY-search)', function(data){
                var field = {'name':data.field.name,'datetime':data.field.datetime,'user_type':data.field.user_type,};
                //执行重载
                table.reload('test', {
                    where: field,page: {curr: 1}
                });
            });
        });
    </script>
@endsection
